package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.dao.SysOperLogDAO;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.service.ISysOperLogService;

/**
 * 操作日志 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Autowired
    private SysOperLogDAO operLogDAO;

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
        operLogDAO.save(operLog);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public Page<SysOperLog> selectOperLogList(SysOperLog operLog,Pageable pageinfo)
    {
        return operLogDAO.findAll(getSpecification(operLog),pageinfo);
    }
    

	@Override
	public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
		return operLogDAO.findAll(getSpecification(operLog));
	}
	
	private Specification<SysOperLog> getSpecification(SysOperLog operLog){
		return (root, criteriaQuery, criteriaBuilder) -> {
			List<Predicate> predicateList = new ArrayList<>();
			
			if (!StringUtils.isEmpty(operLog.getTitle())) {
				predicateList.add(criteriaBuilder.like(root.get("title"), "%"+operLog.getTitle()+"%"));
			}
			if (!StringUtils.isNull(operLog.getBusinessType())) {
				predicateList.add(criteriaBuilder.equal(root.get("businessType"), operLog.getBusinessType()));
			}
			if (!StringUtils.isEmpty(operLog.getBusinessTypes())) {
				predicateList.add(criteriaBuilder.in(root.get("businessType")).value(operLog.getBusinessTypes()));
			}
			if (!StringUtils.isNull(operLog.getStatus())) {
				predicateList.add(criteriaBuilder.equal(root.get("status"),operLog.getStatus()));
			}
			
			if (!StringUtils.isEmpty(operLog.getOperName())) {
				predicateList.add(criteriaBuilder.like(root.get("operName"),"%"+operLog.getOperName()+"%" ));
			}
			
			if (!StringUtils.isNull(operLog.getParams().get("beginTime"))) {
				predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), operLog.getParams().get("beginTime").toString()));
			}
			
			if (!StringUtils.isNull(operLog.getParams().get("endTime"))) {
				predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime"), operLog.getParams().get("endTime").toString()));
			}
			
			Predicate[] p = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(p));
		};
	}

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @SuppressWarnings("unchecked")
	@Override
    public int deleteOperLogByIds(Long[] operIds)
    {
    	operLogDAO.deleteAllByIdInBatch(Arrays.asList(operIds));
        return 1;
    }

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogDAO.getById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogDAO.deleteAllInBatch();
    }
}
